package cn.yangliu.oauth2.config;

import cn.yangliu.oauth2.config.components.JwtRedisTokenStore;
import cn.yangliu.oauth2.config.components.StringSerializationStrategy;
import cn.yangliu.oauth2.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStoreSerializationStrategy;

/**
 * @author 杨柳
 * @date 2019-03-28
 */
@Configuration
public class JsonWebTokenConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    @Primary
    public JwtTokenStore jwtTokenStore() {
        RedisTokenStoreSerializationStrategy strategy = new StringSerializationStrategy(User.class);
        return new JwtRedisTokenStore(jwtAccessTokenConverter(), redisConnectionFactory, strategy);
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
        tokenConverter.setSigningKey("qwertyuiop");
        return tokenConverter;
    }
}
